package fact;

import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import lombok.Data;

import java.util.List;
import java.util.Objects;

/**
 * Fact类型为JSONObject时，比较表达式 测试
 * 适用于规则存储时，即保存为Spel表达式格式(此操作由调用方完成)
 * @author zheng.li
 **/
@Data
public class SpElConditionTransform {

    /**
     * 字段值
     */
    private Object conditionValue;

    /**
     * 字段名
     */
    private String conditionAttr;

    /**
     * 字段名
     */
    private Integer fieldType;

    /**
     * 条件枚举
     */
    private ConditionEnum conditionEnum;



    public SpElConditionTransform(String conditionAttr, Object conditionValue,ConditionEnum conditionEnum) {
        this.conditionAttr = conditionAttr;
        this.conditionValue = conditionValue;
        this.conditionEnum = conditionEnum;
    }


    public String operation() {
        switch (conditionEnum) {
            case EQUAL:
                return equal();
            case NOEQUAL:
                return notEqual();
            case EMPTY:
                return empty();
            case NOEMPTY:
                return notEmpty();
            case ALLINCLUDE:
                return allInclude();
            case INCLUDE:
                return include();
            case IN:
                return in();
            case NOIN:
                return noIn();
            case LIKE:
                return like();
            case NOLIKE:
                return noLike();
            case LESSTHAN:
                return lessThan();
            case LESSEQUAL:
                return lessEqual();
            case GREATERTHAN:
                return greaterThan();
            case GREATEREQUAL:
                return greaterEqual();
            case RANGE:
                return range();
        }
        return "";
    }

    /**
     * 相等
     * @return String
     */
    public String equal() {
        if (conditionValue == null) {
            return "false";
        }
        if (conditionValue instanceof String) {
            return stringEqual();
        } else {
            return objectEqual();
        }
    }


    /**
     * 不等
     * @return String
     */
    public String notEqual() {
        if (conditionValue == null) {
            return "false";
        }
        if (conditionValue instanceof String) {
            return "!" + stringEqual();
        } else {
            return "!" + objectEqual();
        }
    }



    private String stringEqual() {
        return "\"" + conditionValue + "\".equals(#JSONObject.get(\"" + conditionAttr + "\"))";
    }

    private String objectEqual() {
        return  conditionValue + ".equals(#JSONObject.get(\"" + conditionAttr + "\"))";
    }

    /***
     * 为空
     * @return  String
     */
    public String empty() {
        String ret = "#JSONObject.get(\""+ conditionAttr + "\") == null || #JSONObject.get(\"" + conditionAttr + "\").isEmpty()";
         if (Objects.equals(fieldType, FieldTypeEnum.CHECKBOX_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            return ret + " || " + listEmpty();
         }
        return ret;
    }

    /**
     * 不为空
     * @return String
     */
    public String notEmpty() {
        String ret = "#JSONObject.get(\""+ conditionAttr + "\") != null && !#JSONObject.get(\"" + conditionAttr + "\").isEmpty()";
        if (Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            return ret + " && !" + listEmpty();
        }
        return ret;
    }


    private String listEmpty() {
        return "\"[]\".equals(#JSONObject.get(\"" + conditionAttr + "\"))";
    }


    /**
     * 大于
     * @return String
     */
    public String greaterThan() {
        return "#JSONObject.get(\"" + conditionAttr +"\") > " + conditionValue;
    }

    /**
     * 大于等于
     * @return String
     */
    public String greaterEqual() {
        return "#JSONObject.get(\"" + conditionAttr +"\") >= " + conditionValue;
    }

    /**
     * 小于
     * @return String
     */
    public String lessThan() {
        return "#JSONObject.get(\"" + conditionAttr +"\") < " + conditionValue;
    }

    /**
     * 小于等于
     * @return String
     */
    public String lessEqual() {
        return "#JSONObject.get(\"" + conditionAttr +"\") <= " + conditionValue;
    }

    /**
     * 同时包含
     * @return String
     */
    public String allInclude() {
        if (!(conditionValue instanceof List)) {
            return "false";
        }
        String listString = listString((List)conditionValue);
        return "#JSONObject.get(\""  + conditionAttr +"\")"  + ".containsAll(" + listString + ")";
    }

    /**
     * 包含任意一个
     * @return String
     */
    public String include() {
        if (!(conditionValue instanceof List)) {
            return "false";
        }
        String listString = listString((List)conditionValue);

        return "";
    }

    /**
     * 包含
     * @return String
     */
    public String like() {
        if (conditionValue instanceof String) {
            return "#JSONObject.get(\"" + conditionAttr +"\").contains(\"" + conditionValue + "\")";
        }
        return "#JSONObject.get(\"" + conditionAttr +"\").contains(" + conditionValue + ")";
    }

    /**
     * 不包含
     * @return String
     */
    public String noLike() {
        return "!" + like();
    }

    /**
     * 等于任意一个
     * @return String
     */
    public String in() {
        if (!(conditionValue instanceof List)) {
            return "false";
        }
        String listString = listString((List)conditionValue);
        String containAll = listString + ".containsAll(#JSONObject.get(\""  + conditionAttr +"\"))";
        return "@baseHelp.in()";
    }

    private String listString(List list) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (Object o : list) {
            if (list.indexOf(o) != 0) {
                sb.append(",").append(o);
            } else {
                sb.append(o);
            }

        }
        sb.append("}");
        return sb.toString();
    }

    /**
     * 不等于任意一个
     * @return String
     */
    public String noIn() {
        return "";
    }

    /**
     * 大于等于某个值，并且小于某个值
     * @return String
     */
    public String range() {
        return "";
    }


}
